package wang;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 简单模式消费者
 */
public class ConsumerRabbitMQ2 {

    public static void main(String[] args) throws IOException, TimeoutException {

        // 1.创建连接
        ConnectionFactory cf = new ConnectionFactory();

        // 2.配置连接参数
        cf.setHost("47.111.252.79");
        cf.setPort(5672);
        cf.setVirtualHost("/wang");
        cf.setUsername("wang");
        cf.setPassword("wang");

        // 3.创建连接
        Connection connection = cf.newConnection();

        // 4. 创建channel
        Channel channel = connection.createChannel();

        // 5. 创建队列
        /*
        String queue, 队列名称
        boolean durable, 是否持久化
        boolean exclusive,是否独占（只能一个消费者监听）；关闭连接自动删除
        boolean autoDelete, 是否自动删除，没有消费者时自动删除
        Map<String, Object> arguments 参数
         */
        channel.queueDeclare("hello-world", true, false, false, null);

        Consumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

                System.out.println("message....." + new String(body));

            }
        };
        // 6.消费消息
        /*
        String queue 队列名称
        boolean autoAck 是否自动确认
        Consumer callback 回调
         */
        channel.basicConsume("hello-world", true, consumer);

    }

}
